package thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyPrintABC3 {

    static Lock lock = new ReentrantLock();
    static Condition conditionA = lock.newCondition();
    static Condition conditionB = lock.newCondition();
    static Condition conditionC = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                while (true) {
                    lock.lock();
                    System.out.print("A");
                    //Thread.sleep(200L);
                    try {
                        conditionB.signal();

                        conditionA.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();

                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }).start();

        //Thread.sleep(1000L);
        new Thread(() -> {
            try {
                while (true) {
                    lock.lock();
                    System.out.print("B");
                    //Thread.sleep(200L);

                    try {
                        conditionC.signal();
                        conditionB.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();

                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }).start();

        new Thread(() -> {
            try {
                while (true) {
                    lock.lock();
                    System.out.print("C");
                    //Thread.sleep(200L);

                    try {
                        conditionA.signal();
                        conditionC.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    lock.unlock();
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }).start();

        Thread.sleep(30000L);
    }
}